package com.suray.basic.wcs.plc.pojo.oper;

import com.suray.basic.wcs.plc.core.PlcOffset;
import com.suray.basic.wcs.plc.enums.PlcDeviceType;
import com.suray.basic.wcs.plc.enums.Location;
import com.suray.basic.wcs.plc.pojo.DeviceBase;
import com.suray.basic.wcs.utils.Coord;

import java.io.IOException;

/**
 * @author shaozn
 * @date 2
 * @Description 系统监控对象
 */
public class SystemOper extends DeviceBase {

    public static final String DEVICE_NAME = "系统";


    //设备指令
    private static final PlcOffset DEVICE_NO = new PlcOffset(0, null);
    private static final PlcOffset READ_COMPLETION_SIGNAL = new PlcOffset(1, null);
    private static final PlcOffset DATA_QUALIFIED = new PlcOffset(2, null);
    //工位
    private static final PlcOffset STATION_NO = new PlcOffset(3, null);
    private static final PlcOffset RUN_DIRECTION = new PlcOffset(4, null);
    //输送线任务
    private static final PlcOffset PALLET_NO = new PlcOffset(5, null);
    private static final PlcOffset START_STATION_N0 = new PlcOffset(6, null);
    private static final PlcOffset END_STATION_N0 = new PlcOffset(7, null);
    //自动门
    private static final PlcOffset AUTO_DOOR = new PlcOffset(8, null);
    private static final PlcOffset OPEN_AUTO_DOOR = new PlcOffset(9, null);



    private static final PlcOffset SYSTEM_MODEL = new PlcOffset(10, 8);
    private static final PlcOffset AUTO_LOCALITY = new PlcOffset(10, 9);
    private static final PlcOffset AUTO_DISTANCE = new PlcOffset(10, 10);
    private static final PlcOffset SYSTEM_STOP = new PlcOffset(10, 11);
    private static final PlcOffset CONVEY_CRASH_STOP_1 = new PlcOffset(10, 12);
    private static final PlcOffset CONVEY_CRASH_STOP_2 = new PlcOffset(10, 13);
    private static final PlcOffset CONVEY_CRASH_STOP_3 = new PlcOffset(10, 14);
    private static final PlcOffset CHECK_BIT = new PlcOffset(11, null);
    private static final PlcOffset SPARE_1 = new PlcOffset(12, null);
    /**
     * 设备号（写设备号的地址位，写之前需要先检测）
     */
    private Integer deviceNum;
    /**
     * 托盘号（写托盘号的地址位，写之前需要先检测）
     */
    private Integer palletNo;

    /**
     * 系统手动模式
     */
    private boolean systemModel;

    /**
     * 自动本地
     */
    private boolean autoLocality;

    /**
     * 自动远程
     */
    private boolean autoRemote;

    /**
     * 左输送线急停
     */
    private boolean conveyCrashStopL;

    /**
     * 右输送线急停
     */
    private boolean conveyCrashStopR;

    /**
     * 校验位
     */
    private boolean checkBit;

    public SystemOper(PlcDeviceType plcDeviceType, int startNo, String deviceName, int deviceNo, Coord coord,
                      Location location, Coord lockLocation, long deviceId, Integer gridzMax, Integer gridzRelative,
                      int plcId) {
        super(plcDeviceType, startNo, deviceName, deviceNo, coord, location, lockLocation,deviceId,gridzMax,
                gridzRelative, plcId);
    }


    public Boolean getReadCompletionSignal() {
        return readPlc(READ_COMPLETION_SIGNAL) == 1;
    }
    public Integer getDataQualified() {
        return readPlc(DATA_QUALIFIED);
    }
    public Integer getStationNo() {
        return readPlc(STATION_NO);
    }
    public Integer getRunDirection() {
        return readPlc(RUN_DIRECTION);
    }
    public Integer getStartStationNo() {
        return readPlc(START_STATION_N0);
    }
    public Integer getEndStationNo() { return readPlc(END_STATION_N0); }
    public Integer getAutoDoor() {
        return readPlc(AUTO_DOOR);
    }
    public Integer getOpenAutoDoor() {
        return readPlc(OPEN_AUTO_DOOR);
    }

    public boolean openOrCloseDoor(int doorNo, boolean isOpen) {
        boolean result = true;
        int[] doorCmd = {doorNo, isOpen ? 1 : 2};
        try {
            oper("自动门开启或关闭", this.getClass(), AUTO_DOOR.getAddressOffset(), 1000, doorCmd);
        } catch (IOException e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    public Integer getDeviceNum() {
        return readPlc(DEVICE_NO);
    }

    public Integer getPalletNo() {
        return readPlc(PALLET_NO);
    }

    public boolean isSystemModel() {
        return readPlc(SYSTEM_MODEL) == 1;
    }

    public boolean isAutoLocality() {
        return readPlc(AUTO_LOCALITY) == 1;
    }

    public boolean isSystemStop() {
        return readPlc(SYSTEM_STOP) == 1;
    }

    public boolean isAutoDistance() {
        return readPlc(AUTO_DISTANCE) == 1;
    }

    public boolean isConveyCrashStop1() {
        return readPlc(CONVEY_CRASH_STOP_1) == 1;
    }

    public boolean isConveyCrashStop2() {
        return readPlc(CONVEY_CRASH_STOP_2) == 1;
    }

    public boolean isConveyCrashStop3() {
        return readPlc(CONVEY_CRASH_STOP_3) == 1;
    }

    public Integer getCheckBit() {
        return readPlc(CHECK_BIT);
    }
}
